package net.csdn.business.kanban.api.facade;

import cn.hutool.core.collection.CollectionUtil;
import lombok.RequiredArgsConstructor;
import net.csdn.business.kanban.api.enums.BusinessTypeEnums;
import net.csdn.business.kanban.api.service.KanbanDetailService;
import net.csdn.business.kanban.common.domain.DTO.IssueVoDto;
import net.csdn.business.kanban.feign.api.IssueFeignClient;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName IssueFacade
 * @Description TODO
 * @Author liuzhisheng
 * @Date 2023/9/18
 **/
@Service
@RequiredArgsConstructor
public class IssueFacade {
    private final IssueFeignClient issueFeignClient;
    private final KanbanDetailService kanbanDetailService;

    /**
     * @Desc: 获取issue列表，并且过滤掉已经添加至看板的issue
     * @author:liuzhisheng
     * @date: 2023/10/12 10:35
     */
    public List<IssueVoDto> getIssues(Long projectId,String kanbanId,String searchBy) {
        List<IssueVoDto> issueVoDtos = issueFeignClient.issueList(projectId,searchBy);
        /**获取看板下的issueId,过滤掉已经加入看板的issue*/
        List<String> issueIds = kanbanDetailService.getIssueIds(projectId, BusinessTypeEnums.ISSUE.getCode(),kanbanId);
        if(CollectionUtil.isEmpty(issueIds)){
            return issueVoDtos;
        }
        List<IssueVoDto> results = new ArrayList<>();
        issueVoDtos.stream().forEach(bean -> {
            Long count = issueIds.stream().filter(issueid -> bean.getIssueId().equals(issueid)).count();
            if(count == 0 ){
                results.add(bean);
            }
        });
        return results;
    }
}
